package demo.xlj.yuyi.studentapp.common;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import demo.xlj.yuyi.studentapp.R;

public class SheTuanZiXiActivity extends AppCompatActivity {

    @BindView(R.id.ImageView_back)
    ImageView mImageViewBack;
    @BindView(R.id.TextView_Title)
    TextView mTextViewTitle;
    @BindView(R.id.WebView_Zi_Xi_She_Tuan)
    WebView mWebViewZiXiSheTuan;
    @BindView(R.id.progressBar_Zi_Xi_She_Tuan)
    ProgressBar mProgressBarZiXiSheTuan;

    private String url;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0){
                mProgressBarZiXiSheTuan.setVisibility(View.GONE);
//                mWebViewZiXiSheTuan.setVisibility(View.VISIBLE);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_she_tuan_zi_xi);
        ButterKnife.bind(this);
        mTextViewTitle.setText("社团介绍");
        Intent intent = getIntent();

        url = intent.getStringExtra("url");

        url.replace("\r\n","");
        initWebView();
    }

    private void initWebView() {
        //声明WebSettings子类
        WebSettings webSettings = mWebViewZiXiSheTuan.getSettings();

//如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        webSettings.setJavaScriptEnabled(true);
// 若加载的 html 里有JS 在执行动画等操作，会造成资源浪费（CPU、电量）
// 在 onStop 和 onResume 里分别把 setJavaScriptEnabled() 给设置成 false 和 true 即可


        //设置自适应屏幕，两者合用
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小

        //缩放操作
        webSettings.setSupportZoom(false); //支持缩放，默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(false); //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.setDisplayZoomControls(true); //隐藏原生的缩放控件

        //其他细节操作
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
        webSettings.setAllowFileAccess(true); //设置可以访问文件
//        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        mWebViewZiXiSheTuan.setWebChromeClient(new WebChromeClient());
        mWebViewZiXiSheTuan.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                mHandler.sendEmptyMessageDelayed(0,1000);

            }
        });
        mWebViewZiXiSheTuan.loadUrl(url);
    }

    @OnClick(R.id.ImageView_back)
    public void onViewClicked() {
        SheTuanZiXiActivity.this.finish();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mWebViewZiXiSheTuan.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mWebViewZiXiSheTuan != null) {
            mWebViewZiXiSheTuan.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            mWebViewZiXiSheTuan.clearHistory();

            ((ViewGroup) mWebViewZiXiSheTuan.getParent()).removeView(mWebViewZiXiSheTuan);
            mWebViewZiXiSheTuan.destroy();
            mWebViewZiXiSheTuan = null;
        }
    }

}
